package com.zhengsr.drawbroad.utils;

import android.util.Log;

import com.zhengsr.drawbroad.data.StateData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhengshaorui
 * time: 2018/7/22
 */

public class CommandUtils {
    private static final String TAG = "CommandUtils";
    private List<StateData> mUndoStates = new ArrayList<>();
    private List<StateData> mRedoStates = new ArrayList<>();
    private CommandUtils(){

    }




    private static class  Holder{
        static CommandUtils INSTANC = new CommandUtils();
    }
    public static CommandUtils getInstance(){
        return Holder.INSTANC;
    }

    public void add(StateData state){
        mUndoStates.add(state);
        Log.d(TAG, "zsr --> add: "+mUndoStates.size());
    }
    public List<StateData> getBaseState(){
        return mUndoStates;
    }

    public void undo() {
        if (!mUndoStates.isEmpty()){
            int count = mUndoStates.size();
            StateData data = mUndoStates.get(count - 1);
            mUndoStates.remove(count - 1); //去掉undo数据
            Log.d(TAG, "zsr --> undo: "+mUndoStates.size());
            mRedoStates.add(data); //添加到 redo 中
        }
    }
    public void redo() {
        if (!mRedoStates.isEmpty()){
            int count = mRedoStates.size();
            StateData data = mRedoStates.get(count - 1);
            mRedoStates.remove(count - 1);
            mUndoStates.add(data);
        }
    }


    public void clear() {
        mUndoStates.clear();
        mRedoStates.clear();
    }
}
